package org.wendu.wdoa.api;


import org.wendu.wdoa.common.InsertUser;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.page.Page;
import org.wendu.wdoa.dto.MenuDto;
import org.wendu.wdoa.dto.RoleQueryDto;
import org.wendu.wdoa.service.RoleService;
import org.wendu.wdoa.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /*
    传参方式:
       Query String(查询字符串，适用于任何请求)，url后通过？引出的参数
       请求体（payload方式json请求体）适用于post请求，delete，put请求

     */

    @GetMapping("")
    public Result<Page<Role>> roleList(RoleQueryDto dto){

        Page<Role> page = roleService.getRolePage(dto);
        return Result.OK(page);


    }

    /**
     *
     * @param ro_id  角色编号，要求前端以Query String方式，以ro_id为名称的参数
     * @param funIds  角色拥有的权限编号，以请求体（json）的方式传递
     * @return
     */


    @PutMapping("/fun")
    public Result setRoleFuns(@NotNull(message = "角色编号不能为空") Integer ro_id,
                              @NotNull(message = "权限编号不能为空") @RequestBody Integer[] funIds){

        roleService.setRoleFuns(ro_id,funIds);
        return Result.OK();

    }

    @GetMapping("/fun/{roleId}")
    public Result<List<MenuDto>> roleFunList(@PathVariable Integer roleId){

        List<MenuDto> menuList = roleService.getRoleFunList(roleId);
        return Result.OK(menuList);
    }
    //单删
    @DeleteMapping("/delete/{roleId}")
    public Result deleteUser(@PathVariable Integer roleId){
        roleService.deleteUserById(roleId);
        return Result.OK("删除成功");
    }

    //多删
    @DeleteMapping("/deleteMany")
    public Result deleteMore(@RequestBody Integer[] roId){

        roleService.deleteMany(roId);
     return Result.OK("删除成功");
    }
    //新增用户
    @PostMapping("/insertNew")
    public Result insertNew(@RequestBody InsertUser insertUser){
        roleService.insertUser(insertUser.getRo_id(),insertUser.getRo_name());
        return Result.OK("新增用户成功");
    }

    //修改数据
    @PutMapping("/modify")
    public Result updateUser(@RequestBody InsertUser insertUser){
        roleService.updateOldUser(insertUser.getRo_id(),insertUser.getRo_name());
        return Result.OK("修改用户成功");
    }


}
